package pers.youqu.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.youqu.item.dto.SkuDTO;
import pers.youqu.item.mapper.SkuMapper;
import pers.youqu.item.po.Sku;
import pers.youqu.item.service.SkuService;

import java.util.List;
import java.util.Map;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/7 16:09
 * @Description TODO
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {
    @Override
    public List<SkuDTO> findSkuBySkuIds(List<Long> ids) {
        return SkuDTO.convertEntityList(listByIds(ids));
    }

    @Override
    public List<SkuDTO> findSkuBySpuId(Long spuId) {
        return SkuDTO.convertEntityList(query().eq("spu_id",spuId).list());
    }

    @Override
    @Transactional
    public void updateSkuStock(Map<Long, Long> map) {
        map.keySet().forEach(skuId -> {
            update(new UpdateWrapper<Sku>()
                    .eq(skuId != null, "id", skuId)
                    .setSql("stock = stock - " + map.get(skuId)));
        });
    }
}
